#!/usr/bin/perl -w
#purpose: print codons for RRTs combining GC content matching and codon pair bias

$fasta = $ARGV[0];#fasta formatted DNA file (sequence all on 1 line)
open(FASTA,$fasta);

$mult = $ARGV[1];#codon pair multipliers: output of CodonMultipliers.pl
open(MULT,$mult);

#$output = $fasta . "\.codonsgc3";
#Changed to maintain consistency with other scripts
$output = $ARGV[2];
open(OUTPUT,">$output");

@mult = <MULT>;
chomp @mult;
shift @mult;#list of AAs

$CodonNames = shift @mult;#lasi of codons
@CodonNames = split("\t",$CodonNames);
shift @CodonNames;shift @CodonNames;#get rid of tabs at start


@AAnames = ();
push(@AAnames,"F");push(@AAnames,"L");push(@AAnames,"I");push(@AAnames,"M");push(@AAnames,"V");push(@AAnames,"S");push(@AAnames,"P");push(@AAnames,"T");push(@AAnames,"A");push(@AAnames,"Y");push(@AAnames,"H");push(@AAnames,"Q");push(@AAnames,"N");push(@AAnames,"K");push(@AAnames,"D");push(@AAnames,"E");push(@AAnames,"C");push(@AAnames,"W");push(@AAnames,"R");push(@AAnames,"G");push(@AAnames,"X");


#collect mutlipliers
@MultRows = ();
foreach $mline (@mult){
	@mline = split("\t",$mline);
	shift @mline;shift @mline;#get rid of tabs at start
	push(@MultRows,[@mline]);
}

$binsize = 5;#non-overlapping bins: each bin will later be combined with next bin to form partially over-lapping bins
$bins = 100/$binsize;#number of bins

@SeqCount = ();#sequences in each bin
until (scalar @SeqCount == $bins){push(@SeqCount,"0");}

@SumGC3 = ();#sum of %GC3: use for averaging
until (scalar @SumGC3 == $bins){push(@SumGC3,"0");}

@Codons = ();#counts for each codon in whole orfeome
until (scalar @Codons == 64){push(@Codons,"0");}

@CodonBins = ();#counts for each codon in each %GC3 bin
$count = 0;
until ($count == $bins){push @CodonBins, [@Codons];++$count;}

$usedseq = 0;#total sequences used
$badseq = 0;#sequences with incomplete reading frames

while ($sequence = <FASTA>){
	unless ($sequence =~ ">"){#collect sequences, ignore header lines
	
		chomp $sequence;
		$length = length $sequence;
		$codons = $length/3;
		
		#check if sequence is complete reading frame (length = multiple of 3)
		if ($codons == int($codons)){#if complete reading frame
			++$usedseq;
			#determine %GC3 content of sequence: count all Gs and Cs is 3rd codon positions
			$GC3 = 0;
			$CdnEnd = 2;#start with last base of first codon
			until ($CdnEnd > $length){#until last codon: check 3rd pos
				$base3 = substr($sequence,$CdnEnd,1);#last base of current codon
				if ($base3 eq "G" or $base3 eq "C" or $base3 eq "g" or $base3 eq "c"){
					++$GC3;
				}#if last base is GC
				$CdnEnd = $CdnEnd + 3;
			}#until last codon
					
			$perGC3 = 100*($GC3/$codons);#%GC in 3rd codon pos
			$GCbin = int($perGC3/$binsize);#GC bin number (array position)
			
			++$SeqCount[$GCbin];#count sequences in each bin
			$SumGC3[$GCbin] = $SumGC3[$GCbin] + $perGC3;#sum %GC3 values in each bin for averaging
			
			#count occurrences of each codon in sequence:
			$CdnStart = 0;
			until ($CdnStart + 3 > $length){#until last codon
				$codon = substr($sequence,$CdnStart,3);
				
				if ($codon eq "TTT"){++$CodonBins[$GCbin][0];}
				elsif ($codon eq "TTC"){++$CodonBins[$GCbin][1];}
				elsif ($codon eq "TTA"){++$CodonBins[$GCbin][2];}
				elsif ($codon eq "TTG"){++$CodonBins[$GCbin][3];}
				elsif ($codon eq "CTT"){++$CodonBins[$GCbin][4];}
				elsif ($codon eq "CTC"){++$CodonBins[$GCbin][5];}
				elsif ($codon eq "CTA"){++$CodonBins[$GCbin][6];}
				elsif ($codon eq "CTG"){++$CodonBins[$GCbin][7];}
				elsif ($codon eq "ATT"){++$CodonBins[$GCbin][8];}
				elsif ($codon eq "ATC"){++$CodonBins[$GCbin][9];}
				elsif ($codon eq "ATA"){++$CodonBins[$GCbin][10];}
				elsif ($codon eq "ATG"){++$CodonBins[$GCbin][11];}
				elsif ($codon eq "GTT"){++$CodonBins[$GCbin][12];}
				elsif ($codon eq "GTC"){++$CodonBins[$GCbin][13];}
				elsif ($codon eq "GTA"){++$CodonBins[$GCbin][14];}
				elsif ($codon eq "GTG"){++$CodonBins[$GCbin][15];}
				elsif ($codon eq "TCT"){++$CodonBins[$GCbin][16];}
				elsif ($codon eq "TCC"){++$CodonBins[$GCbin][17];}
				elsif ($codon eq "TCA"){++$CodonBins[$GCbin][18];}
				elsif ($codon eq "TCG"){++$CodonBins[$GCbin][19];}
				elsif ($codon eq "AGT"){++$CodonBins[$GCbin][20];}
				elsif ($codon eq "AGC"){++$CodonBins[$GCbin][21];}
				elsif ($codon eq "CCT"){++$CodonBins[$GCbin][22];}
				elsif ($codon eq "CCC"){++$CodonBins[$GCbin][23];}
				elsif ($codon eq "CCA"){++$CodonBins[$GCbin][24];}
				elsif ($codon eq "CCG"){++$CodonBins[$GCbin][25];}
				elsif ($codon eq "ACT"){++$CodonBins[$GCbin][26];}
				elsif ($codon eq "ACC"){++$CodonBins[$GCbin][27];}
				elsif ($codon eq "ACA"){++$CodonBins[$GCbin][28];}
				elsif ($codon eq "ACG"){++$CodonBins[$GCbin][29];}
				elsif ($codon eq "GCT"){++$CodonBins[$GCbin][30];}
				elsif ($codon eq "GCC"){++$CodonBins[$GCbin][31];}
				elsif ($codon eq "GCA"){++$CodonBins[$GCbin][32];}
				elsif ($codon eq "GCG"){++$CodonBins[$GCbin][33];}
				elsif ($codon eq "TAT"){++$CodonBins[$GCbin][34];}
				elsif ($codon eq "TAC"){++$CodonBins[$GCbin][35];}
				elsif ($codon eq "CAT"){++$CodonBins[$GCbin][36];}
				elsif ($codon eq "CAC"){++$CodonBins[$GCbin][37];}
				elsif ($codon eq "CAA"){++$CodonBins[$GCbin][38];}
				elsif ($codon eq "CAG"){++$CodonBins[$GCbin][39];}
				elsif ($codon eq "AAT"){++$CodonBins[$GCbin][40];}
				elsif ($codon eq "AAC"){++$CodonBins[$GCbin][41];}
				elsif ($codon eq "AAA"){++$CodonBins[$GCbin][42];}
				elsif ($codon eq "AAG"){++$CodonBins[$GCbin][43];}
				elsif ($codon eq "GAT"){++$CodonBins[$GCbin][44];}
				elsif ($codon eq "GAC"){++$CodonBins[$GCbin][45];}
				elsif ($codon eq "GAA"){++$CodonBins[$GCbin][46];}
				elsif ($codon eq "GAG"){++$CodonBins[$GCbin][47];}
				elsif ($codon eq "TGT"){++$CodonBins[$GCbin][48];}
				elsif ($codon eq "TGC"){++$CodonBins[$GCbin][49];}
				elsif ($codon eq "TGG"){++$CodonBins[$GCbin][50];}
				elsif ($codon eq "CGT"){++$CodonBins[$GCbin][51];}
				elsif ($codon eq "CGC"){++$CodonBins[$GCbin][52];}
				elsif ($codon eq "CGA"){++$CodonBins[$GCbin][53];}
				elsif ($codon eq "CGG"){++$CodonBins[$GCbin][54];}
				elsif ($codon eq "AGA"){++$CodonBins[$GCbin][55];}
				elsif ($codon eq "AGG"){++$CodonBins[$GCbin][56];}
				elsif ($codon eq "GGT"){++$CodonBins[$GCbin][57];}
				elsif ($codon eq "GGC"){++$CodonBins[$GCbin][58];}
				elsif ($codon eq "GGA"){++$CodonBins[$GCbin][59];}
				elsif ($codon eq "GGG"){++$CodonBins[$GCbin][60];}
				elsif ($codon eq "TAA"){++$CodonBins[$GCbin][61];}
				elsif ($codon eq "TAG"){++$CodonBins[$GCbin][62];}
				elsif ($codon eq "TGA"){++$CodonBins[$GCbin][63];}
				
				$CdnStart = $CdnStart + 3;
				
			}#until last codon
			
		}#if complete reading frame
		
		else {++$badseq;}#else seq had incomplete frame
		
	}#unless header: seq line
	
}#while reading fasta file

close FASTA;

@AverageGC3 = ();#average %GC3 for each %GC bin
@Sequences = ();
#combine each bin (except last) with next bin to form partially overlapping bins:
#0-10,5-15,10-20, etc, up to 90-100
$bin = 0;
$finalbins = 0;
until ($bin == $bins - 1){
	$Seq1 = $SeqCount[$bin];#number of seqs in small non-overlapping bin
	$Seq2 = $SeqCount[$bin + 1];
	$Seqs = $Seq1 + $Seq2;#number of sequences in larger bin
	$Sum1 = $SumGC3[$bin];#sum of %GC3 in small non-overlapping bin
	$Sum2 = $SumGC3[$bin + 1];
	++$finalbins;
	if ($Seqs > 0){$AveGC3 = ($Sum1 + $Sum2)/$Seqs;}else {$AveGC3 = "NA";}#take average %GC3 for bin unless there are no sequences in bin
	push(@AverageGC3,$AveGC3);
	push(@Sequences,$Seqs);
	++$bin;
}

#counts for each AA in each bin:
@stuff = ();
until (scalar @stuff == 21){push(@stuff,"0");}
@AAcounts = ();
$counter = 0;
until ($counter == $finalbins){push(@AAcounts,[@stuff]);++$counter;}

$CodonNum = 0;
@CodonData = ();

#count numbers of each AA and codon
until ($CodonNum == 64){
	$curbin = 0;@row = ();
	until ($curbin == $bins - 1){
		$codon1 = $CodonBins[$curbin][$CodonNum];
		$codon2 = $CodonBins[$curbin + 1][$CodonNum];
		$codons = $codon1 + $codon2;
		#match codon to its AA
		if ($CodonNum == 0){$AAnum = 0;}
		elsif ($CodonNum == 1){$AAnum = 0;}
		elsif ($CodonNum == 2){$AAnum = 1;}
		elsif ($CodonNum == 3){$AAnum = 1;}
		elsif ($CodonNum == 4){$AAnum = 1;}
		elsif ($CodonNum == 5){$AAnum = 1;}
		elsif ($CodonNum == 6){$AAnum = 1;}
		elsif ($CodonNum == 7){$AAnum = 1;}
		elsif ($CodonNum == 8){$AAnum = 2;}
		elsif ($CodonNum == 9){$AAnum = 2;}
		elsif ($CodonNum == 10){$AAnum = 2;}
		elsif ($CodonNum == 11){$AAnum = 3;}
		elsif ($CodonNum == 12){$AAnum = 4;}
		elsif ($CodonNum == 13){$AAnum = 4;}
		elsif ($CodonNum == 14){$AAnum = 4;}
		elsif ($CodonNum == 15){$AAnum = 4;}
		elsif ($CodonNum == 16){$AAnum = 5;}
		elsif ($CodonNum == 17){$AAnum = 5;}
		elsif ($CodonNum == 18){$AAnum = 5;}
		elsif ($CodonNum == 19){$AAnum = 5;}
		elsif ($CodonNum == 20){$AAnum = 5;}
		elsif ($CodonNum == 21){$AAnum = 5;}
		elsif ($CodonNum == 22){$AAnum = 6;}
		elsif ($CodonNum == 23){$AAnum = 6;}
		elsif ($CodonNum == 24){$AAnum = 6;}
		elsif ($CodonNum == 25){$AAnum = 6;}
		elsif ($CodonNum == 26){$AAnum = 7;}
		elsif ($CodonNum == 27){$AAnum = 7;}
		elsif ($CodonNum == 28){$AAnum = 7;}
		elsif ($CodonNum == 29){$AAnum = 7;}
		elsif ($CodonNum == 30){$AAnum = 8;}
		elsif ($CodonNum == 31){$AAnum = 8;}
		elsif ($CodonNum == 32){$AAnum = 8;}
		elsif ($CodonNum == 33){$AAnum = 8;}
		elsif ($CodonNum == 34){$AAnum = 9;}
		elsif ($CodonNum == 35){$AAnum = 9;}
		elsif ($CodonNum == 36){$AAnum = 10;}
		elsif ($CodonNum == 37){$AAnum = 10;}
		elsif ($CodonNum == 38){$AAnum = 11;}
		elsif ($CodonNum == 39){$AAnum = 11;}
		elsif ($CodonNum == 40){$AAnum = 12;}
		elsif ($CodonNum == 41){$AAnum = 12;}
		elsif ($CodonNum == 42){$AAnum = 13;}
		elsif ($CodonNum == 43){$AAnum = 13;}
		elsif ($CodonNum == 44){$AAnum = 14;}
		elsif ($CodonNum == 45){$AAnum = 14;}
		elsif ($CodonNum == 46){$AAnum = 15;}
		elsif ($CodonNum == 47){$AAnum = 15;}
		elsif ($CodonNum == 48){$AAnum = 16;}
		elsif ($CodonNum == 49){$AAnum = 16;}
		elsif ($CodonNum == 50){$AAnum = 17;}
		elsif ($CodonNum == 51){$AAnum = 18;}
		elsif ($CodonNum == 52){$AAnum = 18;}
		elsif ($CodonNum == 53){$AAnum = 18;}
		elsif ($CodonNum == 54){$AAnum = 18;}
		elsif ($CodonNum == 55){$AAnum = 18;}
		elsif ($CodonNum == 56){$AAnum = 18;}
		elsif ($CodonNum == 57){$AAnum = 19;}
		elsif ($CodonNum == 58){$AAnum = 19;}
		elsif ($CodonNum == 59){$AAnum = 19;}
		elsif ($CodonNum == 60){$AAnum = 19;}
		elsif ($CodonNum == 61){$AAnum = 20;}
		elsif ($CodonNum == 62){$AAnum = 20;}
		elsif ($CodonNum == 63){$AAnum = 20;}
		
$AAcounts[$curbin][$AAnum] = $AAcounts[$curbin][$AAnum] + $codons;
		push(@row,$codons);
		++$curbin;
	}#until print all bins
	
	push(@CodonData,[@row]);
	++$CodonNum;
}

print OUTPUT "Bin\tSequences\tGC3 Ave\tAA\tCodons";

$currentbin = 0;
until ($currentbin == $finalbins){
	$codon5p = 0;#number of codon being counted
	$AAlast = 100;
	until ($codon5p == 64){
		$codonsum = $CodonData[$codon5p][$currentbin];
		if ($codon5p == 0){$AAnum = 0;}
		elsif ($codon5p == 1){$AAnum = 0;}
		elsif ($codon5p == 2){$AAnum = 1;}
		elsif ($codon5p == 3){$AAnum = 1;}
		elsif ($codon5p == 4){$AAnum = 1;}
		elsif ($codon5p == 5){$AAnum = 1;}
		elsif ($codon5p == 6){$AAnum = 1;}
		elsif ($codon5p == 7){$AAnum = 1;}
		elsif ($codon5p == 8){$AAnum = 2;}
		elsif ($codon5p == 9){$AAnum = 2;}
		elsif ($codon5p == 10){$AAnum = 2;}
		elsif ($codon5p == 11){$AAnum = 3;}
		elsif ($codon5p == 12){$AAnum = 4;}
		elsif ($codon5p == 13){$AAnum = 4;}
		elsif ($codon5p == 14){$AAnum = 4;}
		elsif ($codon5p == 15){$AAnum = 4;}
		elsif ($codon5p == 16){$AAnum = 5;}
		elsif ($codon5p == 17){$AAnum = 5;}
		elsif ($codon5p == 18){$AAnum = 5;}
		elsif ($codon5p == 19){$AAnum = 5;}
		elsif ($codon5p == 20){$AAnum = 5;}
		elsif ($codon5p == 21){$AAnum = 5;}
		elsif ($codon5p == 22){$AAnum = 6;}
		elsif ($codon5p == 23){$AAnum = 6;}
		elsif ($codon5p == 24){$AAnum = 6;}
		elsif ($codon5p == 25){$AAnum = 6;}
		elsif ($codon5p == 26){$AAnum = 7;}
		elsif ($codon5p == 27){$AAnum = 7;}
		elsif ($codon5p == 28){$AAnum = 7;}
		elsif ($codon5p == 29){$AAnum = 7;}
		elsif ($codon5p == 30){$AAnum = 8;}
		elsif ($codon5p == 31){$AAnum = 8;}
		elsif ($codon5p == 32){$AAnum = 8;}
		elsif ($codon5p == 33){$AAnum = 8;}
		elsif ($codon5p == 34){$AAnum = 9;}
		elsif ($codon5p == 35){$AAnum = 9;}
		elsif ($codon5p == 36){$AAnum = 10;}
		elsif ($codon5p == 37){$AAnum = 10;}
		elsif ($codon5p == 38){$AAnum = 11;}
		elsif ($codon5p == 39){$AAnum = 11;}
		elsif ($codon5p == 40){$AAnum = 12;}
		elsif ($codon5p == 41){$AAnum = 12;}
		elsif ($codon5p == 42){$AAnum = 13;}
		elsif ($codon5p == 43){$AAnum = 13;}
		elsif ($codon5p == 44){$AAnum = 14;}
		elsif ($codon5p == 45){$AAnum = 14;}
		elsif ($codon5p == 46){$AAnum = 15;}
		elsif ($codon5p == 47){$AAnum = 15;}
		elsif ($codon5p == 48){$AAnum = 16;}
		elsif ($codon5p == 49){$AAnum = 16;}
		elsif ($codon5p == 50){$AAnum = 17;}
		elsif ($codon5p == 51){$AAnum = 18;}
		elsif ($codon5p == 52){$AAnum = 18;}
		elsif ($codon5p == 53){$AAnum = 18;}
		elsif ($codon5p == 54){$AAnum = 18;}
		elsif ($codon5p == 55){$AAnum = 18;}
		elsif ($codon5p == 56){$AAnum = 18;}
		elsif ($codon5p == 57){$AAnum = 19;}
		elsif ($codon5p == 58){$AAnum = 19;}
		elsif ($codon5p == 59){$AAnum = 19;}
		elsif ($codon5p == 60){$AAnum = 19;}
		elsif ($codon5p == 61){$AAnum = 20;}
		elsif ($codon5p == 62){$AAnum = 20;}
		elsif ($codon5p == 63){$AAnum = 20;}
		
		$AAtotal = $AAcounts[$currentbin][$AAnum];
		if ($AAtotal > 0){$codonper = 100*($codonsum/$AAtotal);}#percent of AA encoded by this codon
		
		unless ($AAlast == $AAnum){#start new line if starting new AA	
			$ave = $AverageGC3[$currentbin];
			$seqs = $Sequences[$currentbin];
			$aa = $AAnames[$AAnum];
			print OUTPUT "\n$currentbin\t$seqs\t$ave\t$aa";
			$AAlast = $AAnum;
		}#unless still processing same AA, start new line
		
		$codon3p = 0;#number of codon on 3' side for multiplier
		until ($codon3p == 64){
		
			$multiplier = $MultRows[$codon5p][$codon3p];
			if($multiplier eq "NA"){$multiplier = 0};
			if ($AAtotal > 0){$newnum = int(($multiplier*$codonper) + 0.5);}#new codon number equals codon pair multiplier times original codon number
				
			$printed = 0;
			if ($codon3p == 0){print OUTPUT "\t";}
			else {print OUTPUT " ";}
			$codon = $CodonNames[$codon5p];
			if ($AAtotal > 0 and $newnum > 0){
				until ($printed == $newnum){print OUTPUT "$codon";++$printed;}}
			else {print OUTPUT "NNN";}
				
			++$codon3p;
		}#until last multiplier
		
		++$codon5p;
		
	}#until last codon
	
	++$currentbin;
}

#added: print codon totals for each bin, w/o multipliers, to be used for picking 3' codons
$currentbin = 0;
until ($currentbin == $finalbins){
	$codon5p = 0;#number of codon being counted
	$AAlast = 100;
	until ($codon5p == 64){
		$codonsum = $CodonData[$codon5p][$currentbin];
		if ($codon5p == 0){$AAnum = 0;}
		elsif ($codon5p == 1){$AAnum = 0;}
		elsif ($codon5p == 2){$AAnum = 1;}
		elsif ($codon5p == 3){$AAnum = 1;}
		elsif ($codon5p == 4){$AAnum = 1;}
		elsif ($codon5p == 5){$AAnum = 1;}
		elsif ($codon5p == 6){$AAnum = 1;}
		elsif ($codon5p == 7){$AAnum = 1;}
		elsif ($codon5p == 8){$AAnum = 2;}
		elsif ($codon5p == 9){$AAnum = 2;}
		elsif ($codon5p == 10){$AAnum = 2;}
		elsif ($codon5p == 11){$AAnum = 3;}
		elsif ($codon5p == 12){$AAnum = 4;}
		elsif ($codon5p == 13){$AAnum = 4;}
		elsif ($codon5p == 14){$AAnum = 4;}
		elsif ($codon5p == 15){$AAnum = 4;}
		elsif ($codon5p == 16){$AAnum = 5;}
		elsif ($codon5p == 17){$AAnum = 5;}
		elsif ($codon5p == 18){$AAnum = 5;}
		elsif ($codon5p == 19){$AAnum = 5;}
		elsif ($codon5p == 20){$AAnum = 5;}
		elsif ($codon5p == 21){$AAnum = 5;}
		elsif ($codon5p == 22){$AAnum = 6;}
		elsif ($codon5p == 23){$AAnum = 6;}
		elsif ($codon5p == 24){$AAnum = 6;}
		elsif ($codon5p == 25){$AAnum = 6;}
		elsif ($codon5p == 26){$AAnum = 7;}
		elsif ($codon5p == 27){$AAnum = 7;}
		elsif ($codon5p == 28){$AAnum = 7;}
		elsif ($codon5p == 29){$AAnum = 7;}
		elsif ($codon5p == 30){$AAnum = 8;}
		elsif ($codon5p == 31){$AAnum = 8;}
		elsif ($codon5p == 32){$AAnum = 8;}
		elsif ($codon5p == 33){$AAnum = 8;}
		elsif ($codon5p == 34){$AAnum = 9;}
		elsif ($codon5p == 35){$AAnum = 9;}
		elsif ($codon5p == 36){$AAnum = 10;}
		elsif ($codon5p == 37){$AAnum = 10;}
		elsif ($codon5p == 38){$AAnum = 11;}
		elsif ($codon5p == 39){$AAnum = 11;}
		elsif ($codon5p == 40){$AAnum = 12;}
		elsif ($codon5p == 41){$AAnum = 12;}
		elsif ($codon5p == 42){$AAnum = 13;}
		elsif ($codon5p == 43){$AAnum = 13;}
		elsif ($codon5p == 44){$AAnum = 14;}
		elsif ($codon5p == 45){$AAnum = 14;}
		elsif ($codon5p == 46){$AAnum = 15;}
		elsif ($codon5p == 47){$AAnum = 15;}
		elsif ($codon5p == 48){$AAnum = 16;}
		elsif ($codon5p == 49){$AAnum = 16;}
		elsif ($codon5p == 50){$AAnum = 17;}
		elsif ($codon5p == 51){$AAnum = 18;}
		elsif ($codon5p == 52){$AAnum = 18;}
		elsif ($codon5p == 53){$AAnum = 18;}
		elsif ($codon5p == 54){$AAnum = 18;}
		elsif ($codon5p == 55){$AAnum = 18;}
		elsif ($codon5p == 56){$AAnum = 18;}
		elsif ($codon5p == 57){$AAnum = 19;}
		elsif ($codon5p == 58){$AAnum = 19;}
		elsif ($codon5p == 59){$AAnum = 19;}
		elsif ($codon5p == 60){$AAnum = 19;}
		elsif ($codon5p == 61){$AAnum = 20;}
		elsif ($codon5p == 62){$AAnum = 20;}
		elsif ($codon5p == 63){$AAnum = 20;}
		
		$AAtotal = $AAcounts[$currentbin][$AAnum];
		if ($AAtotal > 0){$codonper = 100*($codonsum/$AAtotal);}
		unless ($AAlast == $AAnum){
			unless ($currentbin == 0 and $codon5p == 0){print OUTPUT "\n";}
			$ave = $AverageGC3[$currentbin];
			$seqs = $Sequences[$currentbin];
			$aa = $AAnames[$AAnum];
			print OUTPUT "$currentbin TOTALS\t$seqs\t$ave\t$aa\t";
			$AAlast = $AAnum;
		}#unless still processing same AA, start new line
		
		if ($AAtotal > 0){$newnum = int($codonper + 0.5);}
		else {$newnum = "NNN";}
			
		if ($newnum eq "NNN"){print OUTPUT " $newnum";}#if no AA
		
		else {#if AA is represented in bin, print codons
			$printed = 0;
			$codon = $CodonNames[$codon5p];
			until ($printed == $newnum){print OUTPUT "$codon";++$printed;}
		}#else print codons
			
			++$codon3p;
		
		
		++$codon5p;
		
	}#until last codon
	
	++$currentbin;
}

print "used $usedseq sequences, rejected $badseq with incomplete reading frames\n";

close MULT;
close OUTPUT;
exit;
